package servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.jdbc.pool.DataSource;

import service.UserService;
import session.Cart;
import entity.User;
//import javax.servlet.annotation.WebServlet;


public class Login extends HttpServlet {

	UserService userService=new UserService();

//	 public void init(ServletConfig config) throws ServletException{
//		    try{
//		    	Properties env = new Properties();
//		    	env.put("java.naming.factory.initial", "com.ejbhome.naming.spi.rmi.RMIInitCtxFactory");
//		    	Context ctx = new InitialContext(env);
////		    	homebonus = (BonusHome)ctx.lookup("bonuses");
////		    	homecalc = (CalcHome)ctx.lookup("calcs");	
//		    	CartBean cb = (CartBean)ctx.lookup("carts"); 
//		    	
//		   } catch (Exception NamingException) {
//			   NamingException.printStackTrace();
//		   }
//	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
		if (dispatcher != null) dispatcher.forward(request, response);
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		String password=request.getParameter("password");

		User user=  userService.login(username, password);
		
		if(user!=null){
//			response.setHeader("Content-Type", "text/plain");
//			PrintWriter writer = response.getWriter();
//			writer.write("login succesfully\n");
//			writer.close();

//			Context context;
//			try {
//				context = new InitialContext();
//				CartBean cartA = (CartBean) context.lookup("java:global/simple-stateful/Counter");
//				
//			} catch (NamingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}	       
			
			try {
				InitialContext context = new InitialContext();
				Cart cb = (Cart)context.lookup("YourCart");
				cb.initialize(user);
//				request.setAttribute("cb", cb);
//				response.sendRedirect("home");
				
//				Context initCtx = new InitialContext();
//				Context envCtx = (Context) initCtx.lookup("java:comp/env");
//
//				// Look up our data source
//				Cart cb  = (Cart) envCtx.lookup("bean/CartBean");
//				cb.initialize(user);
//				
				request.setAttribute("userName", user.getUsername());
				
				
			}catch (Exception e) {
				response.sendRedirect("home");
			}
			
		}else{
//			response.setHeader("Content-Type", "text/plain");
//			PrintWriter writer = response.getWriter();
//			writer.write("wrong username or password\n");
//			writer.close();
			request.setAttribute("error", "Login failed!");
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
	}
//	
//	 private static String getLookupName() {
//	        /*
//	         * The app name is the EAR name of the deployed EJB without .ear suffix.
//	         * Since we haven't deployed the application as a .ear, the app name for
//	         * us will be an empty string
//	         */
//	        String appName = "";
//	 
//	        // The module name is the JAR name of the deployed EJB without the .jar
//	        // suffix.
//	        String moduleName = "StatelessSessionBean";
//	 
//	        /*
//	         * AS7 allows each deployment to have an (optional) distinct name. This
//	         * can be an empty string if distinct name is not specified.
//	         */
//	        String distinctName = "";
//	 
//	        // The EJB bean implementation class name
//	        String beanName = AccountBean.class.getSimpleName();
//	 
//	        // Fully qualified remote interface name
//	        final String interfaceName = Account.class.getName();
//	 
//	        // Create a look up string name
//	        String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
//	                + "/" + beanName + "!" + interfaceName + "?stateful";
//	 
//	        return name;
//	 }
}
